<template>
  <div class="container">
    <el-dialog
      width="400px"
      :title="`${data.id ? '修改' : '新增'}目录`"
      @close="closeDialog"
      :visible="showDialog"
      @open="open"
    >
      <el-form
        ref="form"
        :model="formModel"
        :rules="formRules"
        label-width="80px"
        size="small"
      >
        <el-form-item v-if="!$route.query.id" prop="subjectID" label="所属学科">
          <el-select v-model="formModel.subjectID" style="width: 100%">
            <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="tagName" label="目录名称">
          <el-input
            v-model="formModel.tagName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="plain" @click="closeDialog"
          >取消</el-button
        >
        <el-button size="small" type="primary" @click="addTags">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { simple as subjectList } from '@/api/hmmm/subjects'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      formRules: {
        subjectID: [
          { type: 'number', message: '请选择所属学科', tirgger: ['blur', 'change'] }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', tirgger: ['blur', 'change'] }
        ]
      },
      formModel: {
        id: null,
        subjectID: +this.$route.query.id || null,
        tagName: null
      },
      subjectOptions: []
    }
  },
  async created () {
    const { data: subjectArr } = await subjectList()
    this.subjectOptions = subjectArr
  },
  methods: {
    open () {
      if (this.data.id) {
        const { id, subjectID, tagName } = this.data
        this.formModel = { id, subjectID, tagName }
      } else {
        this.formModel = {
          id: null,
          subjectID: +this.$route.query.id || null,
          tagName: null
        }
      }
    },
    addTags () {
      this.$refs.form.validate(async (flag) => {
        if (!flag) return
        if (!this.data.id) {
          const res = await add(this.formModel)
          console.log(res)
          this.$message.success('添加成功')
        } else {
          this.formModel.id = this.data.id
          await update(this.formModel)
          this.$message.success('修改成功')
        }
        this.closeDialog()
        this.$emit('updateTags')
      })
    },
    closeDialog () {
      this.$emit('update:showDialog', false)
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.formModel = {
          id: null,
          subjectID: +this.$route.query.id || null,
          tagName: null
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
